﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Data.Common;
using System.IO;
using DbSharp.Framework.Storage;
using DbSharp.Framework.CaseFile;
using DbSharp.Framework.Driver;
using DbSharp.Framework.DbObject;

namespace DbSharp.Framework.Storage.XmlStorage
{
    class XmlScalarStorage : IScalarStorage
    {
        #region Private members
        private ITestDataSource m_DbTestPlugin;
        private OutputSettingAttribute m_OutputSettingAttr;
        private DbCmdScalarResult m_ScalarResult;
        private string m_ScalarValueText;
        private int m_ScalarValueType;
        private XElement m_DataRootElement;
        #endregion

        #region Private methods


        private XElement CreateResultsetDataElement(string fieldValue)
        {
            /*
<resultset>
<resultsetData>xxx</resultsetData> 
</resultset>
             */
            object fieldElmt;
            string fieldOutputValue;
            fieldOutputValue = StorageTextHelper.EscapeNewlineMark(fieldValue);
            if (NeedUseCData(fieldOutputValue, m_ScalarValueType))
                fieldElmt = new XCData(fieldOutputValue);
            else
                fieldElmt = fieldOutputValue;
            return new XElement("resultsetData", fieldElmt);
        }



        private bool NeedUseCData(string fieldValueText, int valueType)
        {
            if (m_OutputSettingAttr.WrapStringByCDATA)
                return StorageTextHelper.PossibleNeedUseCData(fieldValueText, valueType, m_DbTestPlugin);
            else
                return false;
        }

        #endregion


        public XmlScalarStorage(ITestDataSource dbTestPlugin, OutputSettingAttribute settingAttr)
        {
            m_DbTestPlugin = dbTestPlugin;
            m_OutputSettingAttr = settingAttr; 
        }

        #region IScalarStorage Members


        public void Initialize()
        {
        }

        public void Save(DbCmdScalarResult scalarResult)
        {
            m_ScalarResult = scalarResult;
            m_ScalarValueType = scalarResult.ValueType;
            m_ScalarValueText = m_DbTestPlugin.FieldPersistenceService.ConvertToString(m_ScalarResult.Value, m_ScalarResult.ValueType);

            m_DataRootElement = CreateResultsetDataElement(m_ScalarValueText);
        }


   
        public void Terminate()
        {
        }




        public string ScalarText
        {
            get { return m_ScalarValueText; }
        }


        public XElement ScalarValueXElement
        {
            get { return m_DataRootElement; }
        }


        public List<string> GetOutputFieldNameList()
        {
            //return one dummy list for scalar
            var list = new List<string>();
            list.Add("DUMMY");
            return list;
        }

        public List<FieldMetaData> GetOutputFieldMetaDataQueue()
        {
            //return one dummy list for scalar
            var list = new List<FieldMetaData>();
            list.Add(new FieldMetaData()
                {
                    ActualOutputUpperName = "DUMMY",
                    FieldDataType = m_ScalarResult.ValueType,
                    FieldUpperName = "DUMMY",
                    GivenOutputName = "DUMMY",
                    NullPlaceholder = string.Empty,
                });
            return list;
        }


        //public string ExportToXmlFile()
        //{ 
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append("<resultset>");

        //    //get data structure xml section
        //    sb.Append(Environment.NewLine);
        //    sb.Append(m_StructRootElement.ToString());

        //    //get data value xml section
        //    sb.Append(Environment.NewLine);
        //    sb.Append(m_DataRootElement.ToString());

        //    sb.Append(Environment.NewLine);
        //    sb.Append("</resultset>");

        //    string xmlFile;
        //    StorageNameMaker.PrepareExportXmlFileName(out xmlFile);
        //    File.WriteAllText(xmlFile, sb.ToString(), Encoding.UTF8);
        //    return xmlFile;
        //}

 

       

        #endregion
    }
}
